package com.qf.fmw.utils;

import java.lang.reflect.Field;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class DBManager {

    /*更新操作的封装*/
    public static Integer commonUpdate(String sql, Object... objects) {
        Connection connection = null;
        PreparedStatement ps = null;
        Integer resultSet = null;
        try {
            connection = DruidUtils.getConnection();
            ps = connection.prepareStatement(sql);
            //给占位符赋值
            for (int i = 0; i < objects.length; i++) {
                ps.setObject(i + 1, objects[i]);
            }
            resultSet = ps.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DruidUtils.closeAll(ps, connection);
        }
        return resultSet;
    }

    /*查询单个数据的封装*/
    public static Object commonQuery(String sql, Object... objects) {
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
        Object count = null;
        try {
            connection = DruidUtils.getConnection();
            preparedStatement = connection.prepareStatement(sql);
            if (objects != null) {
                for (int i = 0; i < objects.length; i++) {
                    preparedStatement.setObject(i + 1, objects[i]);
                }
            }
            resultSet = preparedStatement.executeQuery();
            if (resultSet.next()) {
                count = resultSet.getObject(1);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DruidUtils.closeAll(resultSet, preparedStatement, connection);
        }
        return count;
    }


    /*查询对象的封装*/
    public static <T> List<T> commonQuery(String sql, Class<T> clazz, Object... objects) {
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
        List<T> list = new ArrayList<>();
        T t = null;
        String fileName;
        Object fileValue;
        try {
            connection = DruidUtils.getConnection();
            preparedStatement = connection.prepareStatement(sql);
            //判断是否需要给占位符赋值
            if (objects != null) {
                //给占位符赋值
                for (int i = 0; i < objects.length; i++) {
                    preparedStatement.setObject(i + 1, objects[i]);
                }
            }

            resultSet = preparedStatement.executeQuery();
            Field[] fields = clazz.getDeclaredFields();
            while (resultSet.next()) {
                t = clazz.newInstance();
                for (Field field : fields) {
                    field.setAccessible(true);
                    fileName = field.getName();
                    fileName = changeFileName(fileName);
                    fileValue = resultSet.getObject(fileName);
                    field.set(t, fileValue);
                }
                list.add(t);
            }
        } catch (SQLException | InstantiationException | IllegalAccessException e) {
            e.printStackTrace();
        } finally {
            DruidUtils.closeAll(resultSet, preparedStatement, connection);
        }
        return list;
    }

    private static String changeFileName(String fileName) {
        //字符串转换成 char 类型的数组
        char[] chars = fileName.toCharArray();
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < chars.length; i++) {
            //判断是否为大写
            if (Character.isUpperCase(chars[i])) {
                sb.append("_");
            }
            sb.append(chars[i]);
        }
        return sb.toString();
    }
}
